package cn.wolfcode.luowowo.website.web.util;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookiesUtil {
    public static void addCookie(String key, String value, HttpServletResponse response,int time){
        Cookie cookie = new Cookie(key,value);
        cookie.setPath("/");
        cookie.setMaxAge(time);
        response.addCookie(cookie);
    }
    public static String getCookie(HttpServletRequest request,HttpServletResponse response,String key,int time){
        String token = null;
        Cookie[] cookies = request.getCookies();
        if(cookies != null && cookies.length > 0){
            for (Cookie cookie : cookies) {
                if (key.equals(cookie.getName())){
                    token = cookie.getValue();
                    //更新cookie的存活时间到30分钟
                    cookie.setPath("/");
                    cookie.setMaxAge(time);
                    response.addCookie(cookie);
                    break;
                }
            }
        }
        return token;
    }
}
